#include "extern_16ch_adc.h"

void Init_16ch_adc(void)
{
  MY_SPI_DDR|=1<<CLK_PIN|1<<MOSI_PIN|1<<CS_PIN;  
};

void write_contrl_registr(unsigned short byte)
{
 volatile unsigned char i=0;

CLK1;
CHIP_SELECT;	     
  for(i=0;i<16;i++)
  	{
	 if ((byte&(1<<(15-i)))!=0) MOSI1; else MOSI0;		 
		CLK0;		 
		CLK1;				 
	};
CHIP_DESELECT;
};

unsigned short write_shadow_registr(unsigned short byte)
{
 volatile unsigned short tmp=0;
 volatile unsigned char i=0;
 volatile unsigned char _p; 

CLK1;
CHIP_SELECT;
  for(i=0;i<16;i++)
  	{
	 if ((byte&(1<<(15-i)))!=0) MOSI1; else MOSI0;	 	 	 
	 _p=PINB&(1<<PB3); 
	 if (_p!=0) tmp|=1<<(15-i);	 	 
		CLK0;		 
		CLK1;		
	};
CHIP_DESELECT;

 return tmp;
};



unsigned short read_registr_data(unsigned short byte)
{
 volatile unsigned short tmp=0;
 volatile unsigned char _p; 
 volatile int i=0;

CLK1;
CHIP_SELECT;
	     
  for(i=0;i<16;i++)
  	{
	 if ((byte&(1<<(15-i)))!=0) MOSI1; else MOSI0;
	 _p=PINB&(1<<PB3);
	 if ((_p)!=0) tmp|=1<<(15-i);
		CLK0;	     
		CLK1;			 
		 
	};
CHIP_DESELECT;
 return (tmp&0xfff);
};



unsigned short ext_read_adc(unsigned short adc_mux)
{
unsigned short tmp;
 write_contrl_registr(CONF_ADC|adc_mux);
 tmp=read_registr_data(CONF_ADC|adc_mux);
 return tmp;
};
